<?php
class account extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->helper(array('url','form'));		
	}
	
	function index(){
		$cap = captcha('admin_captcha');

		$data = array(
			'captcha' => $cap['image']
		);
		$this->load->view('templates/header');
		$this->load->view('templates/login',$data);
		$this->load->view('templates/footer');
	}
	
	function login(){
		//验证
		$this->load->model('adminuser_model');
		$this->load->helper('encrypt');//加密
		$username = $this->input->post('username');
		$password = encryptUserPassword($this->input->post('password'));
		
		//验证码
		if($postcaptch = $this->input->post('captcha')){
			$this->load->library('session');
			$admin_captcha = $this->session->userdata('admin_captcha');
			
			if(strtolower($postcaptch) != strtolower($admin_captcha['word'])){
				set_session('location','account');
				showmessage('验证码错误!',0,'error');
			}
		}else{
			set_session('location','account');
			showmessage('验证码未填写!',0,'error');
		}
		
		if( !($this->adminuser_model->login($username,$password)) ){
			set_session('location','account');
			showmessage('登录失败，请核对账户密码。',0,'error');
			return false;
		}
		$this->load->library('session');
		if($backrefresh = $this->session->userdata('backurl')){
			set_session('location',$backrefresh);
			$this->session->unset_userdata('backurl');
		}else{
			set_session('crefresh','index');
		}
		showmessage('登录成功!',0,'success');
	}
	
		
	//登出
	function logout(){
		$this->load->library('session');
		$this->session->sess_destroy();
		set_session('location','account');
		showmessage('已退出系统',0,'info',0);
	}
	
	
}